package org.soarsky.wealthchengdu.google.map;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.Log;


import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.ItemizedOverlay.OnFocusChangeListener;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;






public class MarkItemGoogleOverlays extends ItemizedOverlay<MarkGoogleOverlayItem> implements MarkOverlaysInterface,OnFocusChangeListener{

	private OverlayCallBack callBack = null;
	private List<MarkGoogleOverlayItem> list = new ArrayList<MarkGoogleOverlayItem>();
	private MapView mapView = null;
	private Projection projection = null;
	private MapController controller = null;
	private Point point = new Point();
	public MarkItemGoogleOverlays(Drawable arg0,MapView mapView) {
		super(boundCenterBottom(arg0));
		this.mapView = mapView;
		projection = mapView.getProjection();
		controller = mapView.getController();
		this.setOnFocusChangeListener(this);
		if(null!=list) {
			list.clear();
		}
			doPopulate();
	}
	private void doPopulate() {
		populate();

	}
	private GeoPoint createGeoPoint(double latitude,double longitude){
		return new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
	}
	private MarkGoogleOverlayItem getItem(MarkPoint markOverlay){
		MarkGoogleOverlayItem item= new MarkGoogleOverlayItem(createGeoPoint(markOverlay.getLatitude(),markOverlay.getLongitude()), null, null,markOverlay);
		if(markOverlay.getDrawable()!=null){
			item.setMarker(markOverlay.getDrawable());
		}
		return item;
	}
	private int getItemIndex(int id){
		if(null==list)return -1;
		int lenght = list.size();
		for(int i=0;i<lenght;i++) {
			if(id==list.get(i).getMarkPoint().getId())return i;
		}
		return -1;
	}
	@Override
	protected MarkGoogleOverlayItem createItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		if (list != null)
			return list.size();
		else
			return 0;
	}

	public void addMarkOverlay(MarkPoint markOverlay) {
		// TODO Auto-generated method stub
		if(list!=null){
			list.add(getItem(markOverlay));
			doPopulate();
		}
		
	}

	public void setMarkOverlay(MarkPoint markOverlay) {
		// TODO Auto-generated method stub
		if(null==markOverlay)return;
		int index = getItemIndex(markOverlay.getId());
		if(index>-1){
			list.get(index).setMarkPoint(markOverlay);
			list.get(index).setMarker(markOverlay.getDrawable());
			doPopulate();
		}
		
	}

	public void setMarkOverlay(int id, Drawable icon) {
		// TODO Auto-generated method stub
		int index = getItemIndex(id);
		if(index>-1){
			list.get(index).setMarker(icon);
			doPopulate();
		}
	}

	public MarkPoint getMarkOverlay(int id) {
		// TODO Auto-generated method stub
		int index = getItemIndex(id);
		if(index>-1){
			return list.get(index).getMarkPoint();
		}
		return null;
	}

	public void deleteMarkOverlay(int id) {
		// TODO Auto-generated method stub
		int index = getItemIndex(id);
		if(index>-1){
			 list.remove(index);
			 doPopulate();
		}
	}

	public void enableMarkAll() {
		// TODO Auto-generated method stub
		
	}

	public void disableMarkAll() {
		// TODO Auto-generated method stub
		
	}
	public void animateTo(int id) {
		// TODO Auto-generated method stub
		if(null!=controller&&null!=list) {
			int index = getItemIndex(id);
			if(index>-1){
				MarkPoint markPoint= list.get(index).getMarkPoint();
				GeoPoint geoPoint = createGeoPoint(markPoint.getLatitude(), markPoint.getLongitude());
				controller.animateTo(geoPoint);
				geoPoint = null;
			}
		}
	}
	
	public void animateTo(int id, int x, int y) {
		// TODO Auto-generated method stub
		if(null!=controller&&null!=list&&null!=projection) {
			int index = getItemIndex(id);
			if(index>-1){
				MarkPoint markPoint= list.get(index).getMarkPoint();
				GeoPoint geoPoint = createGeoPoint(markPoint.getLatitude(), markPoint.getLongitude());
				projection.toPixels(geoPoint, point);
				geoPoint = projection.fromPixels(point.x+x, point.y+y);
				controller.animateTo(geoPoint);
				geoPoint = null;
				
			}
		}
	}
	
	public void setCenter(int id) {
		// TODO Auto-generated method stub
		if(null!=controller&&null!=list) {
			int index = getItemIndex(id);
			if(index>-1){
				MarkPoint markPoint= list.get(index).getMarkPoint();
				GeoPoint geoPoint = createGeoPoint(markPoint.getLatitude(), markPoint.getLongitude());
				controller.setCenter(geoPoint);
			}
		}
	}
	
	public void setCenter(int id,int x, int y) {
		// TODO Auto-generated method stub
		if(null!=controller&&null!=list&&null!=projection) {
			int index = getItemIndex(id);
			if(index>-1){
				MarkPoint markPoint= list.get(index).getMarkPoint();
				GeoPoint geoPoint = createGeoPoint(markPoint.getLatitude(), markPoint.getLongitude());
				projection.toPixels(geoPoint, point);
				geoPoint = projection.fromPixels(point.x+x, point.y+y);
				controller.setCenter(geoPoint);
				geoPoint = null;
			}
		}
	}
	public void setOnFocus(int id, boolean foucs) {
		// TODO Auto-generated method stub
		if(foucs) {
			setOnFocus( id);
		}else {
			if(getOnFocus())super.setFocus(null);
		}
	}
	public void setOnFocus(int id) {
		// TODO Auto-generated method stub
		if(null!=controller&&null!=list) {
			int index = getItemIndex(id);
			if(index>-1){
				/*清除位置焦点*/
				if(MapOverlaysControlFactory.getInstance().getLocationOverlays()!=null&&
						MapOverlaysControlFactory.getInstance().getLocationOverlays().getOnFocus())
				{
					MapOverlaysControlFactory.getInstance().getLocationOverlays().setOnFocus(false);
				}
				onTap(index);
				super.setFocus(list.get(index));
			}
		}
	}
	
	public boolean getOnFocus() {
		// TODO Auto-generated method stub
		
		return null!=super.getFocus();
	}
	public void setOnTapCallBack( OverlayCallBack callBack){
		// TODO Auto-generated method stub
		this.callBack = callBack;
	}
	public void clearMarkOVerlays() {
		// TODO Auto-generated method stub
		if(null!=list){
			list.clear();
			setLastFocusedIndex(-1);
			doPopulate();
		}
		if(null!=callBack){
			callBack.onLostFocus();
		}
	}

	

	@Override
	public void draw(Canvas canvas, com.google.android.maps.MapView mapView,
			boolean shadow) {
		// TODO Auto-generated method stub
		super.draw(canvas, mapView, shadow);
	}
	@Override
	protected boolean onTap(int arg0) {
		// TODO Auto-generated method stub
		if(null!=callBack){
			callBack.onGotFocus(list.get(arg0).getMarkPoint());
		
		}
		return super.onTap(arg0);
	}
	
	public void distory() {
		// TODO Auto-generated method stub
		if(null!=list){
			list.clear();
			list = null;
			doPopulate();
		}
	}

	public void onFocusChanged(ItemizedOverlay overlay, OverlayItem newFocus) {
		// TODO Auto-generated method stub
		if(null!=callBack&&null==newFocus){
			callBack.onLostFocus();
		}
	}
	
	
	


}
